package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.MarketAddress;
import com.cskaoyan.bean.pojo.MarketAddressExample;
import com.cskaoyan.bean.vo.WxAddressListVO;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Tang_XueCheng
 * @description
 * @since 2022/04/08 20:07
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public WxAddressListVO queryAddressList(Integer userId) {
        //获取用户的id


        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);
        int size = marketAddresses.size();
        WxAddressListVO wxAddressListVO = new WxAddressListVO(10,1,1,size,marketAddresses);
        return wxAddressListVO;
    }

    @Override
    public MarketAddress queryAddressDetail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }

    @Override
    public Integer insertAddress(MarketAddress marketAddress) {
        //如果插入的是默认地址，那么需要查出另外一个默认地址，
        if (marketAddress.getIsDefault()) {
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andIsDefaultEqualTo(true);
            List<MarketAddress> defalutAddresses = marketAddressMapper.selectByExample(example);
            //查询不到，说明目前没有默认地址，那么直接插入即可就直接跳出
            if (defalutAddresses.size()==0) {
                marketAddress.setAddTime(new Date());
                marketAddress.setUpdateTime(new Date());
                marketAddressMapper.insertSelective(marketAddress);
                return  marketAddress.getId();
            }
            //查询到了，需要先把他修改成不是默认的地址，并更新updatetime
            MarketAddress defalutAddress = defalutAddresses.get(0);
            defalutAddress.setIsDefault(false);
            defalutAddress.setUpdateTime(new Date());
            marketAddressMapper.updateByPrimaryKeySelective(defalutAddress);
            //再插入这条数据，并且更新时间
            marketAddress.setAddTime(new Date());
            marketAddress.setUpdateTime(new Date());
            marketAddressMapper.insertSelective(marketAddress);
            return  marketAddress.getId();
        }
        //该地址没有设置成为默认地址，直接插入。
        marketAddress.setAddTime(new Date());
        marketAddress.setUpdateTime(new Date());
        marketAddressMapper.insertSelective(marketAddress);
        return  marketAddress.getId();



    }


    @Override
    public void updateAddress(MarketAddress marketAddress) {
        //如果更新的地址是设置为默认地址，那么需要查出另外一个默认地址，
        if (marketAddress.getIsDefault()) {
            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andIsDefaultEqualTo(true);
            List<MarketAddress> defalutAddresses = marketAddressMapper.selectByExample(example);
            //查询不到，说明目前没有默认地址，那么直接插入即可,然后直接return
            if (defalutAddresses.size() == 0) {
                marketAddress.setUpdateTime(new Date());
                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
                return;
            }
            //查询到了，需要先把他修改成不是默认的地址，并更新updatetime
            MarketAddress defalutAddress = defalutAddresses.get(0);
            defalutAddress.setIsDefault(false);
            defalutAddress.setUpdateTime(new Date());
            marketAddressMapper.updateByPrimaryKeySelective(defalutAddress);
            //再插入这条数据，并且更新时间
            marketAddress.setUpdateTime(new Date());
            marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            return;
        }
        //该更新的地址没有设置成为默认地址，直接更新。
        marketAddress.setUpdateTime(new Date());
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
    }


    @Override
    public void deleteAddress(MarketAddress marketAddress1) {

        marketAddressMapper.updateByPrimaryKeySelective(marketAddress1);
    }

}
